#!/bin/bash

# ReactIM 服务启动脚本
echo "=== ReactIM 服务启动脚本 ==="

# 检查Java环境
if ! command -v java &> /dev/null; then
    echo "❌ Java未安装，请先安装Java 17+"
    exit 1
fi

# 检查Maven环境
if ! command -v mvn &> /dev/null; then
    echo "❌ Maven未安装，请先安装Maven"
    exit 1
fi

# 检查Node.js环境
if ! command -v node &> /dev/null; then
    echo "❌ Node.js未安装，请先安装Node.js"
    exit 1
fi

# 检查npm环境
if ! command -v npm &> /dev/null; then
    echo "❌ npm未安装，请先安装npm"
    exit 1
fi

echo "✅ 环境检查通过"

# 创建日志目录
mkdir -p logs

# 函数：启动后端服务
start_backend_service() {
    local service_name=$1
    local port=$2
    
    echo "🚀 启动 $service_name (端口: $port)..."
    
    cd $service_name
    
    # 编译项目
    echo "📦 编译 $service_name..."
    mvn clean compile -q
    
    if [ $? -ne 0 ]; then
        echo "❌ $service_name 编译失败"
        cd ..
        return 1
    fi
    
    # 启动服务
    echo "▶️  启动 $service_name..."
    nohup mvn spring-boot:run > ../logs/$service_name.log 2>&1 &
    echo $! > ../logs/$service_name.pid
    
    cd ..
    
    # 等待服务启动
    echo "⏳ 等待 $service_name 启动..."
    for i in {1..30}; do
        if curl -s http://localhost:$port/actuator/health > /dev/null 2>&1; then
            echo "✅ $service_name 启动成功 (http://localhost:$port)"
            return 0
        fi
        sleep 2
        echo -n "."
    done
    
    echo ""
    echo "❌ $service_name 启动超时，请检查日志: logs/$service_name.log"
    return 1
}

# 函数：启动前端服务
start_frontend() {
    echo "🚀 启动前端服务..."
    
    cd h5-messaging-client
    
    # 安装依赖
    if [ ! -d "node_modules" ]; then
        echo "📦 安装前端依赖..."
        npm install
        
        if [ $? -ne 0 ]; then
            echo "❌ 前端依赖安装失败"
            cd ..
            return 1
        fi
    fi
    
    # 启动开发服务器
    echo "▶️  启动前端开发服务器..."
    nohup npm run dev > ../logs/frontend.log 2>&1 &
    echo $! > ../logs/frontend.pid
    
    cd ..
    
    # 等待前端服务启动
    echo "⏳ 等待前端服务启动..."
    for i in {1..20}; do
        if curl -s http://localhost:3000 > /dev/null 2>&1; then
            echo "✅ 前端服务启动成功 (http://localhost:3000)"
            return 0
        fi
        sleep 2
        echo -n "."
    done
    
    echo ""
    echo "❌ 前端服务启动超时，请检查日志: logs/frontend.log"
    return 1
}

# 函数：检查服务状态
check_services() {
    echo ""
    echo "=== 服务状态检查 ==="
    
    # 检查用户服务
    if curl -s http://localhost:8081/actuator/health > /dev/null 2>&1; then
        echo "✅ 用户服务 (8081) - 运行中"
    else
        echo "❌ 用户服务 (8081) - 未运行"
    fi
    
    # 检查消息服务
    if curl -s http://localhost:8082/actuator/health > /dev/null 2>&1; then
        echo "✅ 消息服务 (8082) - 运行中"
    else
        echo "❌ 消息服务 (8082) - 未运行"
    fi
    
    # 检查前端服务
    if curl -s http://localhost:3000 > /dev/null 2>&1; then
        echo "✅ 前端服务 (3000) - 运行中"
    else
        echo "❌ 前端服务 (3000) - 未运行"
    fi
}

# 函数：停止所有服务
stop_services() {
    echo ""
    echo "=== 停止所有服务 ==="
    
    # 停止后端服务
    for service in reactim-user-service reactim-message-service frontend; do
        if [ -f "logs/$service.pid" ]; then
            pid=$(cat logs/$service.pid)
            if ps -p $pid > /dev/null 2>&1; then
                echo "🛑 停止 $service (PID: $pid)"
                kill $pid
                rm logs/$service.pid
            fi
        fi
    done
    
    echo "✅ 所有服务已停止"
}

# 主菜单
show_menu() {
    echo ""
    echo "=== ReactIM 服务管理 ==="
    echo "1. 启动所有服务"
    echo "2. 启动用户服务"
    echo "3. 启动消息服务"
    echo "4. 启动前端服务"
    echo "5. 检查服务状态"
    echo "6. 查看日志"
    echo "7. 停止所有服务"
    echo "8. 退出"
    echo ""
    read -p "请选择操作 (1-8): " choice
}

# 主循环
while true; do
    show_menu
    
    case $choice in
        1)
            echo "🚀 启动所有服务..."
            start_backend_service "reactim-user-service" 8081
            start_backend_service "reactim-message-service" 8082
            start_frontend
            check_services
            ;;
        2)
            start_backend_service "reactim-user-service" 8081
            ;;
        3)
            start_backend_service "reactim-message-service" 8082
            ;;
        4)
            start_frontend
            ;;
        5)
            check_services
            ;;
        6)
            echo ""
            echo "=== 可用日志文件 ==="
            ls -la logs/ 2>/dev/null || echo "暂无日志文件"
            echo ""
            read -p "请输入要查看的日志文件名 (如: reactim-user-service.log): " logfile
            if [ -f "logs/$logfile" ]; then
                tail -f "logs/$logfile"
            else
                echo "❌ 日志文件不存在: logs/$logfile"
            fi
            ;;
        7)
            stop_services
            ;;
        8)
            echo "👋 再见！"
            exit 0
            ;;
        *)
            echo "❌ 无效选择，请重新输入"
            ;;
    esac
    
    echo ""
    read -p "按回车键继续..."
done